//
//  MessangerSprite.m
//  MessangerSkeleton
//
//  Created by User-16 on 4/1/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "MessengerSprite.h"
#import "CCAnimation.h"
//#import "CCAction.h"
//#import "CCSpriteFrame.h"
//#import "CCSpriteFrameCache.h"

#import "cocos2d.h"
#import "cocos2d-ui.h"
//#import "CCDirector.h"
#import "TransferDataThroughScenes.h"

@interface MessengerSprite ()

@property CCAnimation* runAnimation;
@property CCAnimation* jumpAnimation;
@property CCAnimation* slideAnimation;

@property CCAction *runAction;
@property CCAction *jumpAction;
@property CCAction *slideAction;

@property CGPoint maxSpeed;
@property CGPoint currentSpeed;
@property CGPoint runningSpeed;
@property CharacterState state;

@end

@implementation MessengerSprite

- (id)initWithType:(MessengerSpriteType)type
{
    NSMutableArray* spriteFrames = [NSMutableArray array];
    if (type == kMessengerSelf) {
        for(int i = 1; i <= kMessengerRunFramesNumber; ++i) {
            CCSpriteFrame* frame =  [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@%d.png",kMessengerFileNameBase, i]];
            [spriteFrames addObject:frame];
        }
    }
    //TODO add sprite sheet for the enemy and implement same for it -- added for testing purposes - rev13
    else{
        for(int i = 1; i <= kMessengerEnemyRunFramesNumber; ++i) {
            CCSpriteFrame* frame =  [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@%d.png",kMessengerEnemyFileNameBase, i]];
            [spriteFrames addObject:frame];
        }
    }
    ;
    self = [super initWithSpriteFrame:[spriteFrames objectAtIndex:0]];
    if (self) {
        
        // set the messenger speed
        self.maxSpeed = kMessengerInitialMaxSpeed;
        
        //initiate animations
        self.runAnimation = [CCAnimation animationWithSpriteFrames:spriteFrames delay:kMessengerRunAnimationDelay*[[CCDirector sharedDirector] fixedUpdateInterval]*60];
        self.jumpAnimation = self.runAnimation;
        self.slideAnimation = self.runAnimation;
        
        // declare actions
        self.runAction = [CCActionRepeatForever  actionWithAction:[CCActionAnimate actionWithAnimation:self.runAnimation]];
        
        self.jumpAction = [CCActionSpawn actions:
                            //[CCActionAnimate actionWithAnimation:self.jumpAnimation],
                            [CCActionJumpBy actionWithDuration:kMessengerJumpDuration
                                                      position:CGPointZero
                                                        height:kMessengerJumpHeight jumps:1],
                           nil];
        
        self.slideAction = [CCActionSpawn actions:
                            [CCActionAnimate actionWithAnimation:self.slideAnimation],
                            nil];
        
        [self run];
    }
    return self;
}

-(void) run
{
    self.state = kStateRunning;
    
    //TODO: chane that
    self.currentSpeed = kMessengerInitialMaxSpeed;
    self.runningSpeed = self.currentSpeed;

    [self stopAllActions];
    [self runAction:self.runAction];
}

- (void) jump
{
    if (self.state != kStateRunning)
        return;
    
    self.state = kStateJumping;
    TransferDataThroughScenes* transferedData = [TransferDataThroughScenes sharedInstance];
    if (transferedData.soundsOn) {
        [[OALSimpleAudio sharedInstance] playEffect:@"laser.wav"];
    }
    [self stopAllActions];
    [self runAction: [CCActionSequence actions: self.jumpAction,
                      [CCActionCallFunc actionWithTarget:self selector:@selector(run)],
                      nil]];
    
}

- (void) slide
{
    if (self.state != kStateRunning)
        return;
    
    self.state = kStateSliding;
    [self stopAllActions];
    [self runAction: [CCActionSequence actions: self.slideAction,
                      [CCActionCallFunc actionWithTarget:self selector:@selector(run)],
                      nil]];
    
}
@end
